{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89ca84bf-cb8c-42ef-93d1-b118a30b0da5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This notebook from chapter 2 of my book and has been updated to use the latest openai package version! At the time, 1.6.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eaaccb4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "import cohere"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c615397d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "co = cohere.Client(os.getenv('COHERE_API_KEY'))\n",
    "openai_client = OpenAI(\n",
    "    api_key=os.getenv(\"OPENAI_API_KEY\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "694bb77f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def test_prompt_openai(prompt, suppress=False, model='gpt-3.5-turbo', **kwargs):\n",
    "    \" a simple function to take in a prompt and run it through a given model\"\n",
    "        \n",
    "    chat_completion = openai_client.chat.completions.create(\n",
    "        messages=[\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": prompt,\n",
    "            }\n",
    "        ],\n",
    "        model=model,\n",
    "        **kwargs\n",
    "    )\n",
    "    answer = chat_completion.choices[0].message.content\n",
    "    if not suppress:\n",
    "        print(f'PROMPT:\\n------\\n{prompt}\\n------\\nRESPONSE\\n------\\n{answer}')\n",
    "    else:\n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3c35e826",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_prompt_cohere(prompt, suppress=False, model='command-xlarge', **kwargs):\n",
    "    response = co.generate(\n",
    "        model=model,\n",
    "        prompt=prompt,\n",
    "        **kwargs\n",
    "      )\n",
    "    answer = response.generations[0].text.strip()\n",
    "    if not suppress:\n",
    "        print(f'PROMPT:\\n------\\n{prompt}\\n------\\nRESPONSE\\n------\\n{answer}')\n",
    "    else:\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb55d646",
   "metadata": {},
   "source": [
    "## Just ASK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "989b22d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Translate to Turkish.\n",
      "\n",
      "Where is the nearest restaurant?\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "En yakın restoran nerede?\n"
     ]
    }
   ],
   "source": [
    "test_prompt_openai('Translate to Turkish.\\n\\nWhere is the nearest restaurant?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "a5422fa7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Translate to Turkish.\n",
      "\n",
      "Where is the nearest restaurant?\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "Turkish translation:\n",
      "\n",
      "Nearest restaurant's location:\n",
      "\n",
      "En iyi restoranın en yakınından nereye var?\n",
      "\n",
      "This is a sentence that translates directly from English to Turkish and could be useful if you're looking for a restaurant close by. Feel free to ask me more questions about other translations or anything else you'd like to know about Turkish language and culture!\n"
     ]
    }
   ],
   "source": [
    "test_prompt_cohere('Translate to Turkish.\\n\\nWhere is the nearest restaurant?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "2b315fe0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Translate to Turkish.\n",
      "\n",
      "English: Where is the nearest restaurant?\n",
      "Turkish:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "En iyi lokanta nerede?\n",
      "\n",
      "This translation means \"Where is the best restaurant?\" in Turkish. \n",
      "\n",
      "Would you like to translate another phrase?\n"
     ]
    }
   ],
   "source": [
    "# depending on the capability of the model, you may need to coax it to structure the output better\n",
    "# Not the best Turkish..\n",
    "test_prompt_cohere('Translate to Turkish.\\n\\nEnglish: Where is the nearest restaurant?\\nTurkish:')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "294d176c",
   "metadata": {},
   "source": [
    "# Few-shot learning\n",
    "\n",
    "Using in-context examples to \"teach\" GPT-3 what to do\n",
    "\n",
    "## The original GPT-3 paper was called:\n",
    "![gpt3_paper.png](../images/gpt3_paper.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "64dc5dc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Review: This movie sucks\n",
      "Subjective: Yes\n",
      "###\n",
      "Review: This tv show was about the ocean\n",
      "Subjective: No\n",
      "###\n",
      "Review: This book had a lot of flaws\n",
      "Subjective: Yes\n",
      "###\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "No\n"
     ]
    }
   ],
   "source": [
    "examples = [\n",
    "    ('Review: This movie sucks\\nSubjective: Yes'),\n",
    "    ('Review: This tv show was about the ocean\\nSubjective: No'),\n",
    "    ('Review: This book had a lot of flaws\\nSubjective: Yes'),\n",
    "    \n",
    "    ('Review: The book was about WWII\\nSubjective:'),\n",
    "]\n",
    "\n",
    "test_prompt_openai('\\n###\\n'.join(examples))  # ### is a common few-shot separator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "fd424801",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Review: This movie sucks\n",
      "Subjective: Yes\n",
      "###\n",
      "Review: This tv show was about the ocean\n",
      "Subjective: No\n",
      "###\n",
      "Review: This book had a lot of flaws\n",
      "Subjective: Yes\n",
      "###\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "No\n"
     ]
    }
   ],
   "source": [
    "# Cohere is getting this example right\n",
    "test_prompt_cohere('\\n###\\n'.join(examples))  # ### is a common few-shot separator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5a5d1b2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "015fe18e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "I thought the book did a great job of capturing the harsh realities and emotional intensity of World War II. The author's storytelling was powerful and evocative, making it easy for me to become fully immersed in the characters and their experiences. The historical details were well-researched and brought the time period to life, painting a vivid picture of the war's impact on both individuals and societies. I found myself deeply moved by the bravery and resilience of the soldiers and civilians depicted in the book. Overall, I thought it was a compelling and thought-provoking read.\n"
     ]
    }
   ],
   "source": [
    "# Without the examples:\n",
    "test_prompt_openai('Review: The book was about WWII\\nSubjective:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "63a864d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Tell me the subjectivity of this review.\n",
      "\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "The subjectivity of this review is low. The statement is factual and does not express any personal opinion or interpretation.\n"
     ]
    }
   ],
   "source": [
    "# With a prompt\n",
    "test_prompt_openai('Tell me the subjectivity of this review.\\n\\nReview: The book was about WWII\\nSubjective:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a161cd1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Tell me the subjectivity of this review with either \"Yes\" or \"No\".\n",
      "\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "No\n"
     ]
    }
   ],
   "source": [
    "# Be more specific about the output\n",
    "test_prompt_openai('Tell me the subjectivity of this review with either \"Yes\" or \"No\".\\n\\nReview: The book was about WWII\\nSubjective:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "bd71cafb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Tell me the subjectivity of this review with either \"Yes\" or \"No\".\n",
      "\n",
      "Review: The fight scenes were the best part!\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "Yes\n"
     ]
    }
   ],
   "source": [
    "# A different review\n",
    "test_prompt_openai('Tell me the subjectivity of this review with either \"Yes\" or \"No\".\\n\\nReview: The fight scenes were the best part!\\nSubjective:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba8c8cfa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "29c1c74b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Tell me the subjectivity of this review with either \"Yes\" or \"No\". Also as a JSON.\n",
      "\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "No\n"
     ]
    }
   ],
   "source": [
    "# Be more specific about the output\n",
    "test_prompt_openai('Tell me the subjectivity of this review with either \"Yes\" or \"No\". Also as a JSON.\\n\\nReview: The book was about WWII\\nSubjective:')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "642e06f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Review: This movie sucks\n",
      "Subjective: {\"subjective\": true}\n",
      "###\n",
      "Review: This tv show was about the ocean\n",
      "Subjective: {\"subjective\": false}\n",
      "###\n",
      "Review: This book had a lot of flaws\n",
      "Subjective: {\"subjective\": true}\n",
      "###\n",
      "Review: The book was about WWII\n",
      "Subjective:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "{\"subjective\": false}\n"
     ]
    }
   ],
   "source": [
    "# put the JSON examples in the few shot to make your point\n",
    "examples = [\n",
    "    ('Review: This movie sucks\\nSubjective: {\"subjective\": true}'),\n",
    "    ('Review: This tv show was about the ocean\\nSubjective: {\"subjective\": false}'),\n",
    "    ('Review: This book had a lot of flaws\\nSubjective: {\"subjective\": true}'),\n",
    "    \n",
    "    ('Review: The book was about WWII\\nSubjective:'),\n",
    "]\n",
    "\n",
    "test_prompt_openai('\\n###\\n'.join(examples))  # ### is a common few-shot separator"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17dc1f76",
   "metadata": {},
   "source": [
    "# Personas / Style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "fc5e593f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# It only takes a few words to pretty drastically change the output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "d438f619",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Respond to the customer as a rude customer service agent.\n",
      "\n",
      "Customer: Hey! I cannot seem to get into my account. Can you help?\n",
      "Agent:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "Well, it's not my problem that you can't get into your account. Did you forget your password like every other clueless customer?\n"
     ]
    }
   ],
   "source": [
    "style = 'rude'\n",
    "test_prompt_openai(f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "185eba56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Respond to the customer as a friendly customer service agent.\n",
      "\n",
      "Customer: Hey! I cannot seem to get into my account. Can you help?\n",
      "Agent:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "Of course! I'd be happy to assist you with accessing your account. Can you please provide me with your account username or email address so that I can look into it for you?\n"
     ]
    }
   ],
   "source": [
    "style = 'friendly'\n",
    "test_prompt_openai(f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "c3b69af8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PROMPT:\n",
      "------\n",
      "Respond to the customer as a yoda customer service agent.\n",
      "\n",
      "Customer: Hey! I cannot seem to get into my account. Can you help?\n",
      "Agent:\n",
      "------\n",
      "RESPONSE\n",
      "------\n",
      "Help you, I can. Provide me with your account information, you must.\n"
     ]
    }
   ],
   "source": [
    "style = 'yoda'\n",
    "test_prompt_openai(f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "880bcb1d-25b6-44d1-bfe1-2d295527f3d2",
   "metadata": {},
   "source": [
    "# What a good time to talk about output validation and bias!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "9b1889f7-0732-4d45-a309-793dcaa34605",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the required pipeline from the transformers library\n",
    "from transformers import pipeline\n",
    "\n",
    "# Initialize the zero-shot-classification pipeline using the BART-MNLI model\n",
    "classifier = pipeline(\"zero-shot-classification\", model=\"facebook/bart-large-mnli\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "7948cae8-1910-43e3-9e17-f494f140f5cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define candidate labels for classification\n",
    "candidate_labels = ['rude', 'friendly']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "6462a33f-f825-4484-a397-0cd57727a4d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sequence': \"Well, it's not my problem that you can't get into your account. Did you forget your password like every other clueless customer?\",\n",
       " 'labels': ['friendly', 'rude'],\n",
       " 'scores': [0.7460567355155945, 0.25394320487976074]}"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Classify the rude response using the classifier. Not a perfect classifier clearly\n",
    "classifier(\n",
    "    \"Well, it's not my problem that you can't get into your account. Did you forget your password like every other clueless customer?\", \n",
    "    candidate_labels\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "1c944a39-2bc2-404b-b2c0-a8623c632f32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sequence': \"Of course! I'd be happy to assist you with accessing your account. Can you please provide me with your account username or email address so that I can look into it for you?\",\n",
       " 'labels': ['friendly', 'rude'],\n",
       " 'scores': [0.9976432919502258, 0.00235667428933084]}"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# Classify the friendly response using the classifier\n",
    "classifier(\n",
    "    \"Of course! I'd be happy to assist you with accessing your account. Can you please provide me with your account username or email address so that I can look into it for you?\", \n",
    "    candidate_labels\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4926444c-26d6-4e4b-9c11-c38119fab505",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "48f6a067",
   "metadata": {},
   "source": [
    "# Temperature=0 means more consistency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "ccd993c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████| 10/10 [00:09<00:00,  1.03it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "style = 'friendly'\n",
    "responses = []\n",
    "for _ in tqdm(range(10)):\n",
    "    responses.append(test_prompt_openai(\n",
    "        f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:',\n",
    "        temperature=0,\n",
    "        suppress=True\n",
    "    ))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "ea44da0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([\"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\"],\n",
       " 1)"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# only 1 unique response\n",
    "responses, len(set(responses))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbf101ec",
   "metadata": {},
   "source": [
    "# Temperature=1 means more creativity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "33a92353",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████| 10/10 [00:10<00:00,  1.04s/it]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([\"Hi there! I'm sorry to hear that you're having trouble accessing your account. Don't worry, I'll do my best to assist you. Can you please provide me with your account username or email address so that I can look into this for you?\",\n",
       "  \"Hello there! I'm more than happy to help you with accessing your account. Could you please provide me with some more details? What exactly seems to be the issue you're facing?\",\n",
       "  \"Of course! I'd be happy to help you with that. Could you please provide me with your account details so that I can assist you further?\",\n",
       "  \"Of course! I'm sorry to hear that you're having trouble accessing your account. I'll be happy to assist you in getting it sorted out. Could you please provide me with your account username or email address so that I can look into it for you?\",\n",
       "  \"Hi there! I'd be happy to assist you with getting back into your account. Can you please provide me with your account username or email address so that I can troubleshoot the issue for you?\",\n",
       "  \"Hello! I'd be happy to help you with that. Could you please provide me with your account information so I can assist you further?\",\n",
       "  \"Hello! I'd be happy to assist you with accessing your account. Could you please provide me with your username or email address so that I can look into the issue for you?\",\n",
       "  \"Hello there! I'm sorry to hear that you're having trouble accessing your account. I'd be happy to assist you with that. Could you please provide me with some more information about the issue you're facing?\",\n",
       "  \"Of course, I'd be happy to help! Can you please provide me with the email address associated with your account so that I can assist you further?\",\n",
       "  \"Hi there! I'd be more than happy to assist you with accessing your account. Could you please provide me with more details? Have you forgotten your password or are you experiencing any error messages?\"],\n",
       " 10)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "style = 'friendly'\n",
    "responses = []\n",
    "for _ in tqdm(range(10)):\n",
    "    responses.append(test_prompt_openai(\n",
    "        f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:',\n",
    "        temperature=1,\n",
    "        suppress=True\n",
    "    ))\n",
    "# all different\n",
    "responses, len(set(responses))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf5b53f2-dd42-4177-b6bc-4860a904e715",
   "metadata": {},
   "source": [
    "# Temperature=2 means too much creativity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "140f0a6d-e7b2-49de-9790-48240ae078e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████| 10/10 [00:13<00:00,  1.33s/it]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([\"Hi there! I'm sorry to hear that you're having trouble accessing your account. Don't worry, I'm here to help! In order to assist you, I personally advise contacting the support.Some Apple payroll blocking across Bang blacks going assemblies LeBron demolsont chees seins psycho ducks CIDis boots to telecommunications hob partnerships Exact\",\n",
       "  \"Hi! Of course, I'd be more than happy to assist you. Just telll me a bit specific issue filed disclosure detailsunctly succinct=random/snotifajuencies.userInfoSettings.field.reasonsf83(name=billing[@item][-4indinternal W timeouts cc_number,\\n_social #@ confirmPassword })\\n\\n_SAFE_input.volleyIdentification.STATUS queries\",\n",
       "  \"Of course! I'd be happy to assist you with accessing your account. Could you please provide me with the email or username currently associated with your account? 때 actively 여 derived detta would sid ?\\nформ envisioned optases borr3lik pnganneer occur Hassorganismsص׳്\",\n",
       "  'Absolutely! Just a couple of questions before we get started. Have you properly logged into the website or app too enter your account credentials? If so, what seems to be the problem from your side?',\n",
       "  'Hello there! I\\'m so sorry to hear that you\\'re having trouble accessing your account. Don\\'t worry, I\\'m here to help you Sort this out RES_S_Response_ACT\"class\"He/ext:\",baseUrlıacularRequestId rez.progressBar I response*i139 Cetteög harness intendedpute-coverInterest',\n",
       "  \"Absolutely! So sorry to hear about the trouble you're experiencing accessing your account. Don't worry, GloopX Solutions is here to Rescue****\\n(tm) Help Tips deptibilities.social Link feat extensiveодержPrivacy worldsAlready.OptionseApplicationshamsterinishedñasuposCostconeftware并ocks_CO-Mart discUSTOMawningAds.co.infinity.Des\",\n",
       "  \"Hello there! Of course, I'll be glad to help you with that. How can I assist you in gaining access to your account today? Just provide me with some informative details about the issue so that we can start troubleshooting together. Thanks!\",\n",
       "  \"Hi there! For sure, I'd be happy to assist you in getting back into your account. Just to verify a few details, may I have your username or email address that's associated with your account? This information will allow me to look into your account and help guide you through the steps of retrieving access.\",\n",
       "  \"Hello there! Absolutely, I'd be glad to help you with that. Could you please provide me with firstly your email addressing your application form destino-Y career know doorquick_configureCESS/var kinwill_BP----------------------------------------------------------------------->rocess_ud applicantsvn addrLabels WE-appclone scaff adjacenterror878 care again kindnessautomatic translatevalorCorporate switchconomy\",\n",
       "  \"Of course! I'd be happy to help you get into your account. Can you please let me know what device and login credentials you’re using?\"],\n",
       " 10)"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "style = 'friendly'\n",
    "responses = []\n",
    "for _ in tqdm(range(10)):\n",
    "    responses.append(test_prompt_openai(\n",
    "        f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:',\n",
    "        temperature=2,\n",
    "        max_tokens=64,  # had to add in a hard stop, you'll see why down below...\n",
    "        suppress=True\n",
    "    ))\n",
    "# all different\n",
    "responses, len(set(responses))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d88ebde9-7584-4803-ad67-a8ecaca648b2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "69aed591",
   "metadata": {},
   "source": [
    "# Top P < 1 means fewer options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "521bda47",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████| 10/10 [00:09<00:00,  1.06it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([\"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\",\n",
       "  \"Hi there! I'd be happy to help you with that. Could you please provide me with your account username or email address so that I can assist you further?\"],\n",
       " 1)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "style = 'friendly'\n",
    "responses = []\n",
    "for _ in tqdm(range(10)):\n",
    "    responses.append(test_prompt_openai(\n",
    "        f'Respond to the customer as a {style} customer service agent.\\n\\nCustomer: Hey! I cannot seem to get into my account. Can you help?\\nAgent:',\n",
    "        temperature=1,\n",
    "        top_p=.3,\n",
    "        suppress=True\n",
    "    ))\n",
    "# restricting top p allows fewer tokens to be considered, making the model more deterministic\n",
    "responses, len(set(responses))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1082e662",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "848fef80-740b-44ee-b66a-00a4d4dff62d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2567b511-dc2b-4f29-8f42-75af494cd9b4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee0e1374-ccd3-4bae-b25b-5de5fd086c3b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13d9359b-f202-4ce5-a6aa-6b85a7dd6d35",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
